package com.forlove.app.qselect.answer.widght;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.forlove.app.R;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 答案页面
 * Created by yjl on 2017/10/12.
 */

public class AnswerFriendVew extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private View mRootView;
    private List<MineFriendEntity> friendEntities = new ArrayList<>(); //联系人列表
    private AnswerItemView answer_view_a;
    private AnswerItemView answer_view_b;
    private AnswerItemView answer_view_c;
    private AnswerItemView answer_view_d;
    private MineFriendEntity entityA;
    private MineFriendEntity entityB;
    private MineFriendEntity entityC;
    private MineFriendEntity entityD;
    private OnSelectedItemListener onSelectedItemListener;
    private MineFriendEntity selectedFriend;//选择
    private List<AnswerItemView> answerItemViews = new ArrayList<>();

    public AnswerFriendVew(Context context) {
        super(context);
        initView(context);
    }

    public AnswerFriendVew(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AnswerFriendVew(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setOnSelectedItemListener(OnSelectedItemListener onSelectedItemListener) {
        this.onSelectedItemListener = onSelectedItemListener;
    }

    private void initView(Context mContext) {
        this.mContext = mContext;
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.layout_answer_view, this);
        answer_view_a = (AnswerItemView) mRootView.findViewById(R.id.answer_view_a);
        answer_view_b = (AnswerItemView) mRootView.findViewById(R.id.answer_view_b);
        answer_view_c = (AnswerItemView) mRootView.findViewById(R.id.answer_view_c);
        answer_view_d = (AnswerItemView) mRootView.findViewById(R.id.answer_view_d);
        answer_view_a.setOnClickListener(this);
        answer_view_b.setOnClickListener(this);
        answer_view_c.setOnClickListener(this);
        answer_view_d.setOnClickListener(this);
        answerItemViews.add(answer_view_a);
        answerItemViews.add(answer_view_b);
        answerItemViews.add(answer_view_c);
        answerItemViews.add(answer_view_d);
    }

    public void updateAnswerFriend(List<MineFriendEntity> circleFriends) {
        this.friendEntities.clear();
        this.friendEntities.addAll(circleFriends);
        clearSelected();
        if (this.friendEntities.size() >= 4) {
            entityA = friendEntities.get(0);
            entityB = friendEntities.get(1);
            entityC = friendEntities.get(2);
            entityD = friendEntities.get(3);
            for (int i = 0; i < answerItemViews.size(); i++) {
                AnswerItemView answerItemView = answerItemViews.get(i);
                answerItemView.setAnswerTitle(friendEntities.get(i).getAvaliableName());
            }

        }
    }

    /**
     * 选择的四个好友
     *
     * @return
     */
    public List<MineFriendEntity> getAnswerFriend() {
        return friendEntities;
    }

    /**
     * 清除圈选选项
     */
    public void clearSelected() {

        for (AnswerItemView answerItemView : answerItemViews) {
            answerItemView.selectCircleFriend(false);
        }
        selectedFriend = null;
        hideBackView();
    }

    public MineFriendEntity getSelectedFirend() {

        return selectedFriend;
    }

    public void setCircleFriend(int pos) {
        if (onSelectedItemListener != null) {
            selectedFriend = friendEntities.get(pos);
            AnswerItemView answerItemView = answerItemViews.get(pos);
            answerItemView.selectCircleFriend(true);
            for (int i = 0; i < answerItemViews.size(); i++) {
                if (i != pos) {
                    answerItemViews.get(i).selectCircleFriend(false);
                }
            }
            onSelectedItemListener.selectedFriend(selectedFriend, answerItemView.getFriendCircled(), pos);
        }

    }

    @Override
    public void onClick(View v) {
        //TODO 目前是单选好友,后面多选再注掉这句
        selectedFriend = null;
        switch (v.getId()) {
            case R.id.answer_view_a:
                if (answer_view_a.getFriendCircled()) {
                    answer_view_a.selectCircleFriend(false);
                    selectedFriend = null;
                } else {
                    answer_view_a.selectCircleFriend(true);
                    selectedFriend = entityA;
                }
                if (onSelectedItemListener != null) {
                    onSelectedItemListener.selectedFriend(entityA, answer_view_a.getFriendCircled(), 0);
                }
                answer_view_b.selectCircleFriend(false);
                answer_view_c.selectCircleFriend(false);
                answer_view_d.selectCircleFriend(false);
                break;
            case R.id.answer_view_b:
                if (answer_view_b.getFriendCircled()) {
                    answer_view_b.selectCircleFriend(false);
                    selectedFriend = null;
                } else {
                    answer_view_b.selectCircleFriend(true);
                    selectedFriend = entityB;
                }
                answer_view_a.selectCircleFriend(false);
                answer_view_c.selectCircleFriend(false);
                answer_view_d.selectCircleFriend(false);
                if (onSelectedItemListener != null) {
                    onSelectedItemListener.selectedFriend(entityB, answer_view_b.getFriendCircled(), 1);
                }
                break;
            case R.id.answer_view_c:
                if (answer_view_c.getFriendCircled()) {
                    answer_view_c.selectCircleFriend(false);
                    selectedFriend = null;
                } else {
                    answer_view_c.selectCircleFriend(true);
                    selectedFriend = entityC;
                }
                answer_view_a.selectCircleFriend(false);
                answer_view_b.selectCircleFriend(false);
                answer_view_d.selectCircleFriend(false);
                if (onSelectedItemListener != null) {
                    onSelectedItemListener.selectedFriend(entityC, answer_view_c.getFriendCircled(), 2);
                }
                break;
            case R.id.answer_view_d:
                if (answer_view_d.getFriendCircled()) {
                    answer_view_d.selectCircleFriend(false);

                    selectedFriend = null;
                } else {
                    answer_view_d.selectCircleFriend(true);
                    selectedFriend = entityD;
                }
                answer_view_a.selectCircleFriend(false);
                answer_view_b.selectCircleFriend(false);
                answer_view_c.selectCircleFriend(false);
                if (onSelectedItemListener != null) {
                    onSelectedItemListener.selectedFriend(entityD, answer_view_d.getFriendCircled(), 3);
                }
                break;
        }
    }

    public void showChoicePercent(float[] scores, String backColor) {
        showChoicePercent(scores[0], scores[1], scores[2], scores[3], backColor);
    }

    public void showChoicePercent(float a, float b, float c, float d, String backColor) {
        answer_view_a.showBackView(Color.parseColor(backColor), a);
        answer_view_b.showBackView(Color.parseColor(backColor), b);
        answer_view_c.showBackView(Color.parseColor(backColor), c);
        answer_view_d.showBackView(Color.parseColor(backColor), d);

    }

    public void hideBackView() {
        answer_view_a.hideBackView();
        answer_view_b.hideBackView();
        answer_view_c.hideBackView();
        answer_view_d.hideBackView();

    }

    public interface OnSelectedItemListener {
        void selectedFriend(MineFriendEntity friendEntity, boolean selected, int pos);
    }


    public void setAnswerViewA(String answer_view_a, boolean selected) {
        this.answer_view_a.setAnswerTitle(answer_view_a);
        this.answer_view_a.selectCircleFriend(selected);
        this.answer_view_a.setOnClickListener(null);
    }

    public void setAnswerViewB(String answer_view_b, boolean selected) {
        this.answer_view_b.setAnswerTitle(answer_view_b);
        this.answer_view_b.selectCircleFriend(selected);
        this.answer_view_b.setOnClickListener(null);
    }

    public void setAnswerViewC(String answer_view_c, boolean selected) {
        this.answer_view_c.setAnswerTitle(answer_view_c);
        this.answer_view_c.selectCircleFriend(selected);
        this.answer_view_c.setOnClickListener(null);
    }

    public void setAnswerViewD(String answer_view_d, boolean selected) {
        this.answer_view_d.setAnswerTitle(answer_view_d);
        this.answer_view_d.selectCircleFriend(selected);
        this.answer_view_d.setOnClickListener(null);
    }


    public void setCircleFriend(String[] names, String[] mobileList, String[] answerMobile, String mColor) {

        if (names.length > 3 && mobileList.length > 3) {
            String mobileA = mobileList[0];
            for (String seleName : answerMobile) {
                if (seleName.equals(mobileA)) {
                    setAnswerViewA(names[0], true);
                    showCrownImage(0);
                    break;
                } else {
                    setAnswerViewA(names[0], false);
                    hideFriendName(0, mColor);
                }
            }

            String mobileB = mobileList[1];
            for (String seleName : answerMobile) {
                if (seleName.equals(mobileB)) {
                    setAnswerViewB(names[1], true);
                    showCrownImage(1);
                    break;
                } else {
                    setAnswerViewB(names[1], false);
                    hideFriendName(1, mColor);
                }
            }

            String mobileC = mobileList[2];
            for (String sleName : answerMobile) {
                if (sleName.equals(mobileC)) {
                    setAnswerViewC(names[2], true);
                    showCrownImage(2);
                    break;
                } else {
                    setAnswerViewC(names[2], false);
                    hideFriendName(2, mColor);
                }
            }

            String mobileD = mobileList[3];
            for (String sleName : answerMobile) {
                if (sleName.equals(mobileD)) {
                    setAnswerViewD(names[3], true);
                    showCrownImage(3);
                    break;
                } else {
                    setAnswerViewD(names[3], false);
                    hideFriendName(3, mColor);
                }
            }

        }
    }

    /**
     * 设置选项是否可以被点击
     */
    public void setAvaliableSelected(boolean isAvaliable) {
        if (isAvaliable) {
            for (AnswerItemView answerItemView : answerItemViews) {
                answerItemView.setOnClickListener(this);
            }
        } else {
            for (AnswerItemView answerItemView : answerItemViews) {
                answerItemView.setOnClickListener(null);
            }
        }
    }

    public void hideFriendName(int pos, String mColor) {
        if (answerItemViews != null && answerItemViews.size() > pos) {
            answerItemViews.get(pos).hideFriendName(Color.parseColor(mColor));
        }
    }

    public void showCrownImage(int pos) {
        switch (pos) {
            case 0:
                answer_view_a.showFriendName();
                answer_view_a.showCrownView();
                break;
            case 1:
                answer_view_b.showFriendName();
                answer_view_b.showCrownView();
                break;
            case 2:
                answer_view_c.showFriendName();
                answer_view_c.showCrownView();
                break;
            case 3:
                answer_view_d.showFriendName();
                answer_view_d.showCrownView();
                break;
        }
    }
}

